package com.neusoft.neuiotms.sap;

import java.io.IOException;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

import org.activiti.spring.SpringAsyncExecutor;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.activiti.spring.boot.AbstractProcessEngineAutoConfiguration;
import org.activiti.spring.boot.ActivitiProperties;
import org.activiti.spring.boot.DataSourceProcessEngineAutoConfiguration;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

/**
 * activiti自带的
 * {#org.activiti.spring.boot.JpaProcessEngineAutoConfiguration}，参数注入会发现多个存在的bean，重写
 *
 * @author Joram Barrez
 * @author Josh Long
 */
@Configuration
@AutoConfigureBefore(DataSourceProcessEngineAutoConfiguration.class)
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
public class JpaProcessEngineAutoConfiguration {

    @Configuration
    @ConditionalOnClass(name = "javax.persistence.EntityManagerFactory")
    @EnableConfigurationProperties(ActivitiProperties.class)
    public static class JpaConfiguration extends AbstractProcessEngineAutoConfiguration {

        @Override
        @Bean("mSpringAsyncExecutor")
        public SpringAsyncExecutor springAsyncExecutor(@Qualifier("mTaskExecutor") TaskExecutor taskExecutor) {
            return new SpringAsyncExecutor(taskExecutor, this.springRejectedJobsHandler());
        }

        @Bean
        @ConditionalOnMissingBean
        public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
            return new JpaTransactionManager(emf);
        }

        @Bean
        @ConditionalOnMissingBean
        public SpringProcessEngineConfiguration springProcessEngineConfiguration(
                DataSource dataSource, EntityManagerFactory entityManagerFactory,
                PlatformTransactionManager transactionManager,
                @Qualifier("mSpringAsyncExecutor") SpringAsyncExecutor springAsyncExecutor)
                throws IOException {

            final SpringProcessEngineConfiguration config = this.baseSpringProcessEngineConfiguration(dataSource,
                    transactionManager, springAsyncExecutor);
            config.setJpaEntityManagerFactory(entityManagerFactory);
            config.setTransactionManager(transactionManager);
            config.setJpaHandleTransaction(false);
            config.setJpaCloseEntityManager(false);
            return config;
        }
    }

}
